package fr.inpl_nancy.mines.andromines.bluetooth;

import fr.inpl_nancy.mines.andromines.envemu.*;

/**
 * Bluetooth interface
 * 
 * @author Olivier Genest
 * @version 0.1
 */
public class Bluetooth implements EnvironmentEmulationInterface {

	
	private BluetoothInterface upperLayer;
	
	//bluetooth address... to study.. using MAC ?
	private int from;
	
	/**
	 * Default constructor
	 * 
	 * @param address Address of the bluetooth node
	 */
	public Bluetooth(int address) {
		this.from = address;
		//Registering as the upper layer of the Environment Emulation layer
		//with the name "bluetooth"
		EnvironmentEmulation.getEnvironmentEmulation().register("bluetooth", this);
	}
	
	/**
	 * Advanced constructor 
	 * 
	 * @param address Address of the bluetooth node
	 * @param name Name to use to register with the lower layer
	 */
	public Bluetooth(int address, String name) {
		this.from = address;
		EnvironmentEmulation.getEnvironmentEmulation().register(name, this);
	}
	
	
	
	
	/**
	 * Function that has to be used by the upper layer to register with the bluetooth layer
	 * 
	 * @param bi Upper layer that implement "BluetoothInterface"
	 */
	public void setUpperLayer(BluetoothInterface bi) {
		upperLayer = bi;
	}
	
	/**
	 * Function that allow to send a message to a node through the bluetooth network
	 * 
	 * @param to Destination address
	 * @param message Message to send
	 * @return Boolean depending wether it has been sent succesfully or not
	 */
	public void sendMessage(int to, String message) {
		EnvironmentEmulation.getEnvironmentEmulation().sendSocket("<message from="+from+" to="+to+">"+message+"</message>");
	}
	
	/**
	 * Function to connect to a node in the bluetooth network
	 * 
	 * @param to Node address
	 * @return Boolean depending wether it has been connected succesfully or not
	 */
	public void connect(int to) {
		EnvironmentEmulation.getEnvironmentEmulation().sendSocket("<connect from="+from+">"+to+"</connect>");
	}
	
	/**
	 * Function used by the layer below to traet the received datas
	 * 
	 * @param data Received data
	 */
	public void treat(String data) {
		//TODO : treating the received data
		
		//here we will use the BluetoothInterface upperLayer 
		//to send data to the upper layer
		
	}
	
	
	
	
}
